<template>
  <el-container class="home_container">
    <!-- 头部区域(logo标志以及退出按钮) -->
    <el-header>
      <div class="home_head">
        <img src="../assets/images/leo.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="handleExit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏(menu菜单) -->
      <el-aside :width="iscollapse ? '64px':'200px'">
        <!-- 控制菜单栏展开与隐藏 -->
        <div class="ControlCollapse" @click="ControlCollapse">|||</div>
        <!-- 菜单栏 -->
        <el-menu
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activeIndex"
        >
          <el-submenu :index="item.id+''" v-for="item in MenuList" :key="item.id">
            <template slot="title">
              <i :class="MenuIcon[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <el-menu-item
              :index="'/'+subitem.path+''"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="handleActiveMenu('/'+subitem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{subitem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 各个组件的显示区域 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 菜单栏数据
      MenuList: [],
      // 菜单栏图标
      MenuIcon: {
        "125": "el-icon-user-solid",
        "103": "el-icon-s-help",
        "101": "el-icon-s-goods",
        "102": "el-icon-s-order",
        "145": "el-icon-s-marketing"
      },
      // 控制菜单栏展开与隐藏
      iscollapse: false,
      // 控制那个菜单项处于选中状态(字体颜色变蓝)
      activeIndex: ""
    };
  },
  created() {
    this.getMenuList();
    this.activeIndex = window.sessionStorage.getItem("activeIndex");
  },
  methods: {
    // 退出系统
    handleExit() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取菜单栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.MenuList = res.data;
    },
    // 控制菜单栏展开与隐藏
    ControlCollapse() {
      this.iscollapse = !this.iscollapse;
    },
    // 点击菜单项后把活跃的index保存到本地中
    handleActiveMenu(activeIndex) {
      window.sessionStorage.setItem("activeIndex", activeIndex);
      this.activeIndex = activeIndex;
    }
  }
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  .home_head {
    display: flex;
    justify-content: center;
    align-items: center;

    color: #fff;
    font-size: 20px;
    img {
      width: 60px;
      height: 60px;
      border-radius: 50%;
    }
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
i[class^="el-icon"] {
  margin-right: 15px;
}
.ControlCollapse {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>